var webUrl = "http://47.104.210.57:9999";


// 获取地址栏参数
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

// 查询用户是否登录过

var userId = window.localStorage.getItem('userId')
var userType = window.localStorage.getItem('userType')


function login(userType,userId) {
    let url= webUrl+'/Customer/getCustomerId/'+userId;
    if(userType==2){
        url = webUrl+'/supplier/getid/'+userId;
    }
    $.ajax({
        type:'get',
        url:url,
        success:function(data){
            if(userType==1){
                $('.userName').html(data.content.name)
            }else{
                $('.userName').html(data.content.registrationName)
                $('#uploading').show()
            }
            $('.noLogin').hide()
            $('.account').show()

        }
    })
}
if(userId){
    login(userType,userId)
}
// 注册
// 验证手机号
var isR = false;
$('.RuserPhone').bind('input propertychange',function(){
    console.log($('.RuserPhone').val())
    if($('.RuserPhone').val().trim()==''){
        alert('数据不能为空')
        $('.RuserPhone').val('')
        return false;
    }
    $.ajax({
        url:webUrl + '/user/booleanPhone',
        type:'get',
        data:{
            userPhone:$('.RuserPhone').val()
        },
        success:function(data){
            if(data.code == 0){
                isR = true;
                $('.RuserPhone').next().html(data.msg)
            }else{
                isR = false;
                $('.RuserPhone').next().html('')
            }
            return false;
        }
    })
})
// 验证密码是否一致
$('#password2').bind('input propertychange',function(){
    let pas1 = $('#password1').val()
    let pas2 = $('#password2').val()
    if(pas1!=pas2){
        $('#password2').next().html('两次密码不一致，请重新输入')
        return
    }else{
        $('#password2').next().html('')
    }
})
// 获取验证码
$('#code').click(function(){
    if(!isR){
        let userPhone = $('.RuserPhone').val()
        if(userPhone.trim()==''){
            alert('数据不能为空')
            $('.RuserPhone').val('')
            return false;
        }
        $.ajax({
            type:"post",
            url:webUrl +'/user/sendPhoneAcquistion',
            data:{userPhone:userPhone},
            success:function(data){
                $('#code').html('正在发送中--')

                setTimeout(function(){
                    if(data.content){
                        $('#code').html('发送成功！')
                    }else{
                        $('#code').html('发送失败！再来一次')
                    }
                }, 3000);
            }
        })
    }else{
        alert('请确认手机号是否正确')
    }

})
function goMore(userId){
    $.confirm({
        title: '确认',
        content: '想要查看更多信息需要去完善您的个人信息',
        type: 'green',
        icon: 'glyphicon glyphicon-question-sign',
        buttons: {
            ok: {
                text: '确定',
                btnClass: 'btn-primary',
                action: function() {
                    let searchUrl = encodeURI("person.html?userId=" + userId); //使用encodeURI编码
                    location.href = searchUrl;
                }
            },
            cancel: {
                text: '取消',
                btnClass: 'btn-primary',
                action: function() {
                    window.location.reload(); //刷新页面
                }
            }
        }
    })
}
// 提交注册数据
$('.subBtn').click(function(){
    let userPhone = $('.RuserPhone').val()
    let pas1 = $('#password1').val()
    let pas2 = $('#password2').val()
    let per = $('#person').val()
    let code = $('.code').val()
    console.log(per)
    if(isR){
        alert('手机号已经注册过')
    }
    if(userPhone==''||pas1==''||pas2==''){
        alert('数据不能为空')
        return false;
    }
    if(pas1!=pas2){
        alert('两次密码不一致')
        return false;
    }
    if(code.trim==''){
        alert('验证码必须填写')
    }
    $.ajax({
        type:'post',
        url:webUrl+'/user/adduser',
        data:{
            userPhone:userPhone,
            userPassword:pas1,
            userType:per,
            Acquistion:code
        },
        success:function(data){
            console.log(data)
            if(data.code==1){
                alert('注册成功')
                console.log(data)
                window.localStorage.setItem('userType',data.content.userType)
                $(".modal").click();
                if(!data.content.userTypeId){
                    console.log('没有登录过')
                    let searchText = data.content.userId
                    goMore(searchText)
                }else{
                    window.localStorage.setItem('userId',data.content.userTypeId)
                    // window.location.reload();
                }
            }else{
                alert(data.message)
            }
        }
    })
})
// 登录
$('.LBtn').click(function(){
    let userPhone = $('.LuserPhone').val()
    let pas = $('.password').val()
    if(userPhone.trim==''||pas.trim==''){
        alert('数据不能为空')
    }
    $.ajax({
        type:'post',
        url:webUrl+'/user/loginByPhone',
        data:{
            userPhone:userPhone,
            userPassword:pas
        },
        success:function(data){
            console.log(data)
            window.localStorage.setItem('userType',data.content.userType)
            if(data.code==1){
                if(!data.content.userTypeId){
                    let searchText = data.content.userId
                    goMore(searchText)
                    return false;
                }

                window.localStorage.setItem('userId',data.content.userTypeId)
                console.log(userId)
                login(data.content.userType,data.content.userTypeId)
                $('#exampleModal').modal('hide');
            }else{
                alert(data.msg)
            }

        }
    })
})
// 退出
$('.exit').click(function(){
    window.localStorage.clear()
    window.location.reload()
})
//获取导航
$.ajax({
    type:'get',
    url:webUrl + '/goodsType/findAll',
    dataType:'json',
    success:function (data) {
        let dataJson = data.content;
        // 一级导航加载
        for (let i=0;i<dataJson.length;i++){
            let option = `<li class="firstLev" goodsId = '` +dataJson[i].goodsTypeId + `'><a href='#' title='' class='dropdown'><span class='menu-img'><img src='images/icons/menu/01.png' alt=''></span>
                <span class='menu-title'>`+dataJson[i].goodsTypeName+`</span></a></li>`;
            $('.menu').eq(0).append(option)
        }

    }
})
// 点击一级导航获取添加二级导航
$(document).on('click','.firstLev',function () {
    let goodsId = $(this).attr('goodsId');
    $.ajax({
        type:'get',
        url:webUrl + '/goodsTypeTwo/findByGoodsTypeId',
        data:{
            goodsTypeId:goodsId
        },
        dataType:'json',
        success:function (data) {
            let secendDataJson = data.content;
            $('.drop-menu').html();
            let secendOptions =``;
            $('.drop-menu').css({'opacity':'1','visibility':'visible','transform':'translateX(0%)'})
            for (let i=0;i<secendDataJson.length;i++) {
                secendOptions += `<li><a href="goods.html?secendId=`+secendDataJson[i].goodsTypeTwoId+`" title="" class="dropdown"><span class="menu-title">` + secendDataJson[i].goodsTypeTwoName + `</span></a></li>`
            }
            $('.drop-menu').html(secendOptions)
        }
    })
})
// 二级导航展示
$('#mega-menu').mouseleave(function () {
    $('.drop-menu').css({'opacity':'0','visibility':'hidden','transform':'translateX(-10%)'})
})


